package com.example.contact;

import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

public class ReadContact {
    public static void main(String[] args) throws IOException {
        Contact contact = Contact.parseFrom(new FileInputStream("src/main/java/com/example/contact/contact.bin"));

//        System.out.println(contact.toString());
        printPeopleInfo(contact);
    }

    private static void printPeopleInfo(Contact contact) throws InvalidProtocolBufferException {
        for (PeopleInfo peopleInfo : contact.getPeopleInfoList()) {
            System.out.println(peopleInfo.getName());
            System.out.println(peopleInfo.getAge());
            for (PeopleInfo.Phone phone : peopleInfo.getPhoneList()) {
                System.out.print(phone);
                System.out.println(phone.getPhoneType().name());
            }

            if (peopleInfo.hasData() && peopleInfo.getData().is(Address.class)) {
                Address address = peopleInfo.getData().unpack(Address.class);
                System.out.println(address.getHomeAddress());
                System.out.println(address.getCompanyAddress());
            }

            switch (peopleInfo.getOtherContactCase()) {
                case QQ:
                    System.out.println(peopleInfo.getQq());
                    break;
                case WECHAT:
                    System.out.println(peopleInfo.getWechat());
                    break;
                default:
                    System.out.println("其他联系方式设置错误");
            }

            for (Map.Entry<String, String> entry : peopleInfo.getRemarkMap().entrySet()) {
                System.out.println(entry.getKey() + "----" + entry.getValue());
            }
        }
    }
}
